package fx.mine.active.component.blog;

import cn.hutool.core.collection.CollectionUtil;
import com.dtflys.forest.Forest;
import fx.mine.active.component.blog.api.SubscribeAPI;
import io.github.palexdev.materialfx.controls.MFXListView;
import io.github.palexdev.materialfx.controls.cell.MFXListCell;
import javafx.collections.ObservableList;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;
import lombok.extern.log4j.Log4j2;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@Log4j2
public class BlogPane extends AnchorPane {

    public BlogPane(double width, double height) {
        setPrefSize(width, height);


        List<SubscribeAPI.Subscribe> subscribeList = parseSubscribe();
        List<HBox> hBoxList = subscribeList.stream().map(subscribe -> {
            HBox hBox = new HBox();
            ObservableList<Node> children = hBox.getChildren();
            children.add(new ImageView(subscribe.getUserAvatar()));
            children.add(new Text(subscribe.getTitle()));
            children.add(new Text(subscribe.getAuthorName()));
            hBox.setUserData(subscribe.getDataUrl());
            return hBox;
        }).toList();
        MFXListView<HBox> listView = new MFXListView<>();
        AnchorPane.setLeftAnchor(listView, 50.0);
        AnchorPane.setRightAnchor(listView, 50.0);
        AnchorPane.setTopAnchor(listView, 50.0);
        AnchorPane.setBottomAnchor(listView, 50.0);

        listView.setCellFactory(new Function<HBox, MFXListCell<HBox>>() {
            @Override
            public MFXListCell<HBox> apply(HBox hBox) {
                MFXListCell<HBox> cell = new MFXListCell<>(listView, hBox);
                cell.setAlignment(Pos.CENTER_LEFT);
                cell.updateItem(hBox);
                return cell;
            }
        });

        listView.getItems().addAll(hBoxList);
        getChildren().add(listView);

        getStyleClass().add("blogPane");
        getStylesheets().add(getClass().getResource("/css/blogPane.css").toExternalForm());
    }

    private List<SubscribeAPI.Subscribe> parseSubscribe() {
        SubscribeAPI client = Forest.client(SubscribeAPI.class);
        String str = client.blogList();

        Document document = Jsoup.parse(str);
        Elements elements = document.select("#daily > div > .posts > .post");

        if (CollectionUtil.isEmpty(elements)) {
            return Collections.emptyList();
        }
        List<SubscribeAPI.Subscribe> list = new ArrayList<>();

        for (Element element : elements) {
            Elements select1 = element.select(".post > .content");
            String dataUrl = select1.attr("data-url");
            String title = element.select(".post > .content > h3 > a").get(0).text();
            String authorName = element.select(".post > .user-info > .info > h4").get(0).text();
            String avatarUrl = element.select(".post > .user-info > .user-avatar > a > img").attr("src");
            SubscribeAPI.Subscribe subscribe = new SubscribeAPI.Subscribe();
            subscribe.setTitle(title);
            subscribe.setAuthorName(authorName);
            subscribe.setUserAvatar(avatarUrl);
            subscribe.setDataUrl(dataUrl);
            list.add(subscribe);
        }
        return list;
    }
}
